package network_programming.np2_nonblocking_communication;

import java.io.IOException;
import java.net.SocketOption;
import java.nio.channels.*;

/**
 * 打印每一种类型的通道所支持的所有选项
 * */
public class OptionPrinter {
    private static void printOptions(NetworkChannel channel){
        /*
         * Object        所有类的超类
         * public final native Class<?> getClass()    返回此对象的运行时类
         *
         * Class<T>
         * public String getSimpleName()             返回源代码中给出的基础类的简单名称
         * */
        System.out.println(channel.getClass().getSimpleName() + " supports:");
        for (SocketOption<?> option : channel.supportedOptions()) {
            try {
                System.out.println(option.name() + ": " + channel.getOption(option));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println();
        try {
            channel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        printOptions(SocketChannel.open());
        printOptions(ServerSocketChannel.open());
        printOptions(AsynchronousSocketChannel.open());
        printOptions(AsynchronousServerSocketChannel.open());
        printOptions(DatagramChannel.open());
    }
}
